package com.example.unitestexample;

import com.example.utils.ManifestFileExtractor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Properties;

/**
 * @author ME
 */
@Component
public class ManifestFileExtractingRunner implements CommandLineRunner {
    @Autowired
    private ConfigurableEnvironment configurableEnvironment;


    @Override
    public void run(String... args) throws Exception {
        MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
        Map<String, String> result = ManifestFileExtractor.extract();
        Properties properties = new Properties();
        if (null != result) {
            for (Map.Entry<String, String> entry : result.entrySet()) {
                String key = "info." + entry.getKey();
                properties.setProperty(key, entry.getValue());
            }
        }
        if (!properties.isEmpty()) {
            propertySources.addFirst(new PropertiesPropertySource("infoProperties", properties));
        }
    }
}
